package com.sqx.modules.orders.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.annotation.LoginUser;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.orders.service.OrdersService;
import com.sqx.modules.orders.service.PriceRuleService;
import com.sqx.modules.orders.vo.CalculatePrice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Api(value = "订单-用户端", tags = {"订单-用户端"})
@RequestMapping("/app/orders")
public class AppOrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UserService userService;

    @PostMapping("/insertOrders")
    @ApiOperation("生成订单")
    public Result insertOrders(@RequestBody Orders orders){
        return ordersService.insertOrders(orders);
    }

    @PostMapping("/insertOrdersFixedStroke")
    @ApiOperation("生成订单")
    public Result insertOrdersFixedStroke(@RequestBody Orders orders){
        return ordersService.insertOrdersFixedStroke(orders);
    }

    @PostMapping("/payOrders")
    @ApiOperation("支付订单")
    public Result payOrders(Long ordersId){
        return ordersService.payOrders(ordersId);
    }

    @Login
    @GetMapping("/selectMyOrdersList")
    @ApiOperation("查询我的订单")
    public Result selectMyOrdersList(Integer page,Integer limit,@RequestAttribute Long userId,Integer status){
        IPage<Orders> ordersIPage = ordersService.page(new Page<>(page, limit), new QueryWrapper<Orders>()
                .eq("user_id", userId).eq(status != null, "status", status).orderByDesc("create_time"));
        return Result.success().put("data",new PageUtils(ordersIPage));
    }

    @Login
    @GetMapping("/selectOrdersDetails")
    @ApiOperation("查询订单详情")
    public Result selectOrdersDetails(Long ordersId,Long driverUserId,Long driverRouteId){
        return ordersService.selectOrdersDetails(ordersId,driverUserId,driverRouteId);
    }

    @Login
    @GetMapping("/selectOrdersList")
    @ApiOperation("查询订单列表")
    public Result selectOrdersList(Integer page,Integer limit,Integer appointmentType,Integer appointmentNum,Integer status,String shipProvince,
                                   String shipCity,String shipDistrict,String shipAddress,String deliveryProvince,String deliveryCity,
                                   String deliveryDistrict,String deliveryAddress,String userName,String phone,String shipUserName,String shipUserPhone,
                                   String driverName,String driverPhone,Integer sort,Long driverRouteId,String ol,String od,
                                   Long driverId,String ordersNo,Integer orderType,Long driverUserId,Long driverRouteIds,Integer isFixedStroke){
        return ordersService.selectOrdersList(page, limit, appointmentType, appointmentNum, status, shipProvince, shipCity, shipDistrict, shipAddress,
                deliveryProvince, deliveryCity, deliveryDistrict, deliveryAddress, userName, phone, shipUserName, shipUserPhone, driverName, driverPhone,
                sort,driverRouteId,ol,od,driverId,ordersNo,orderType,driverUserId,driverRouteIds,isFixedStroke);
    }

    @Login
    @GetMapping("/selectOrdersCount")
    @ApiOperation("统计该地区的订单数量")
    public Result selectOrdersCount(String deliveryProvince,String deliveryCity,String deliveryDistrict){
        String[] split = deliveryDistrict.split(",");
        Integer count = ordersService.count(new QueryWrapper<Orders>().eq("status", 2)
                .eq("delivery_province", deliveryProvince).eq("delivery_city", deliveryCity));
        List<Integer> districtCountList=new ArrayList<>();
        for(String str:split){
            Integer counts = ordersService.count(new QueryWrapper<Orders>().eq("status", 2)
                    .eq("delivery_province", deliveryProvince).eq("delivery_city", deliveryCity).eq("delivery_district",str));
            districtCountList.add(counts);
        }
        Map<String,Object> result=new HashMap<>();
        result.put("count",count);
        result.put("districtCountList",districtCountList);
        return Result.success().put("data",result);
    }

    @Login
    @PostMapping("/takingOrders")
    @ApiOperation("接单")
    public Result takingOrders(Long ordersId,@RequestAttribute Long userId,Long driverRouteId){
        return ordersService.takingOrders(ordersId, userId,null,driverRouteId);
    }

    @Login
    @PostMapping("/proceedOrders")
    @ApiOperation("抵达进行中")
    public Result proceedOrders(Long ordersId,String code){
        return ordersService.proceedOrders(ordersId,code);
    }

    @Login
    @PostMapping("/completeOrders")
    @ApiOperation("完成订单")
    public Result completeOrders(Long ordersId){
        return ordersService.completeOrders(ordersId);
    }

    @Login
    @PostMapping("/cancelOrders")
    @ApiOperation("取消订单")
    public Result cancelOrders(Long ordersId){
        return ordersService.cancelOrders(ordersId);
    }

    @Login
    @GetMapping("/getOrderSubscript")
    @ApiOperation("订单角标")
    public Result getOrderSubscript(@RequestAttribute("userId") Long userId) {
        return Result.success().put("data", ordersService.getOrderSubscript(userId));
    }

    @Login
    @GetMapping(value = "/getMyHaveOrder")
    @ApiOperation("用户获取正在进行中的订单")
    public Result getMyHaveOrder(@RequestAttribute Long userId) {
        return Result.success().put("data", ordersService.list(new QueryWrapper<Orders>().eq("user_id", userId).in("status", 2,3,4)));
    }

    @Login
    @GetMapping(value = "/getMasterHaveOrder")
    @ApiOperation("师傅获取正在进行中的订单")
    public Result getMasterHaveOrder(@RequestAttribute Long userId) {
        return Result.success().put("data", ordersService.count(new QueryWrapper<Orders>().eq("driver_user_id", userId).in("status", 3,4)));
    }

    @Login
    @ApiOperation("师傅收入统计 完成订单数统计")
    @GetMapping(value = "/findIncome")
    public Result findIncome(@RequestAttribute Long userId) {
        return ordersService.findIncome(userId);
    }

    @Login
    @GetMapping("/selectTeamUserList")
    @ApiOperation("获取团队列表")
    public Result selectTeamUserList(@RequestAttribute Long userId, Integer page, Integer limit, Integer type){
        UserEntity user = userService.selectUserById(userId);
        return ordersService.selectTeamUserList(page,limit,user.getInvitationCode(),type,userId);
    }

    @Login
    @GetMapping("/selectTeamStatistics")
    @ApiOperation("团队统计")
    public Result selectTeamStatistics(@LoginUser UserEntity user) {
        Double oneTeamMoney = ordersService.selectOrdersMoneyCountByUserId(user.getUserId(), 1);
        Integer oneTeamCount = ordersService.selectUserCountByInvitationCode(user.getInvitationCode(), 1);
        Double twoTeamMoney = ordersService.selectOrdersMoneyCountByUserId(user.getUserId(), 2);
        Integer twoTeamCount = ordersService.selectUserCountByInvitationCode(user.getInvitationCode(), 2);
        Integer teamCount = oneTeamCount + twoTeamCount;
        BigDecimal teamMoney = BigDecimal.valueOf(oneTeamMoney).add(BigDecimal.valueOf(twoTeamMoney));

        Map<String, Object> result = new HashMap<>();
        result.put("teamMoney", teamMoney);
        result.put("teamCount", teamCount);
        result.put("oneTeamMoney", oneTeamMoney);
        result.put("oneTeamCount", oneTeamCount);
        result.put("twoTeamMoney", twoTeamMoney);
        result.put("twoTeamCount", twoTeamCount);
        return Result.success().put("data", result);
    }


}